package cn.wsalix.admin.repository;

import java.util.List;

import javax.persistence.QueryHint;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;
import org.springframework.data.repository.query.Param;

import cn.anlaser.repository.CommonRepository;
import cn.wsalix.admin.entity.Permit;

public interface PermitRepository extends CommonRepository<Permit> {

	@Query("select o from Permit o where o.code =:code")
	public List<Permit> findByCode(@Param("code") String code);
	
	@Query("select u from Permit u where u.name = :name")
	public List<Permit> findByName(@Param("name") String name);
	
	
	@Query(value = "select u from Permit u")
	@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value = "true") })
	public List<Permit> findAllWithCache();

	@Query("select u from Permit u where u.name = :name")
	public Permit findByNameOne(@Param("name") String name);
/*
	@Query("select p from Permit p,RolePermit rp,SysRole r,UserRole ur  where ur.user.id = :userId and r.id=ur.role.id and rp.role.id=r.id and rp.permit.id=p.id")
	public List<Permit> findByUserId(@Param("userId") Long userId);
*/


	@Query(nativeQuery = true,value = "select permit.* from (select admin_permit.*,menu_permit.menu_id as menuPermit from WOW_ADMIN_PERMIT admin_permit    left outer join (select mp.* from WOW_ADMIN_MENU_PERMIT mp  where mp.menu_id = ?1) menu_permit on admin_permit.id= menu_permit.permit_id) permit    where menuPermit is null")
	public List<Permit> findUnsByMenuId(@Param("menuId") Long menuId);

}
